package com.tledu.stu_manager.service;

import com.tledu.stu_manager.dao.StudentDao;
import com.tledu.stu_manager.exception.CustomException;
import com.tledu.stu_manager.model.Student;

import java.util.List;

/**
 * @author Antg
 * @date 2021/7/3118:12
 */
public class StudentService {
    private final StudentDao studentDao;

    public StudentService(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    /**
     * 根据id查询学生
     *
     * @author Antg
     * @date 2021/7/31 19:18
     */
    public Student findStuById(Integer id) throws CustomException {
        return studentDao.getStuById(id);
    }

    /**
     * 查询所有学生
     *
     * @author Antg
     * @date 2021/7/31 18:14
     */
    public List<Student> findList(Integer pageNum, Integer pageSize) throws CustomException {
        return studentDao.findWithPage(pageNum, pageSize);
    }

    /**
     * 删除学生(将删除标志设置为1)
     *
     * @author Antg
     * @date 2021/7/31 18:31
     */

    public void delStudent(Integer id) throws CustomException {
        studentDao.delStudent(id);
    }

    /**
     * 编辑
     *
     * @author Antg
     * @date 2021/7/31 18:32
     */
    public void modifyStudent(Student student) throws CustomException {
        studentDao.modifyStudent(student);
    }

    /**
     * 添加学生
     *
     * @author Antg
     * @date 2021/7/31 18:34
     */

    public void addStudent(Student student) throws CustomException {
        studentDao.addStudent(student);
    }

    /**
     * 批量删除
     *
     * @author Antg
     * @date 2021/7/31 20:27
     */
    public void batchDelStudent(String idList) throws CustomException {
        String[] tempList = idList.split(",");
        Integer[] list = new Integer[tempList.length];
        for (int i = 0; i < tempList.length; i++) {
            list[i] = Integer.parseInt(tempList[i]);
        }
        studentDao.batchDelStudent(list);
    }

    /**
     * 根据用户名模糊查询
     *
     * @author Antg
     * @date 2021/7/31 23:40
     */
    public List<Student> fuzzyQueryByUsername(String username) throws CustomException {
        return studentDao.fuzzyQueryStudent(username);
    }

    /**
     * 查询总页数
     *
     * @author Antg
     * @date 2021/8/2 11:32
     */

    public Integer getNum() throws CustomException {
        return studentDao.getNum();
    }
}
